package com.huanxing.cloud.mall.api.handler.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.huanxing.cloud.common.core.constant.CacheConstants;
import com.huanxing.cloud.common.core.desensitization.MobilePhoneDesensitization;
import com.huanxing.cloud.mall.api.service.IDistributionUserService;
import com.huanxing.cloud.mall.api.service.IUserInfoService;
import com.huanxing.cloud.mall.api.utils.HxTokenHolder;
import com.huanxing.cloud.mall.common.constant.MallUserConstants;
import com.huanxing.cloud.mall.common.dto.HxTokenInfo;
import com.huanxing.cloud.mall.common.dto.UserLoginDTO;
import com.huanxing.cloud.mall.common.entity.UserInfo;
import com.huanxing.cloud.mall.common.enums.ClientTypeEnum;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component("SMS_LOGIN")
@AllArgsConstructor
public class SmsLoginHandler extends AbstractLoginHandler {

	private IUserInfoService userInfoService;

	private final RedisTemplate<String, String> redisTemplate;

	private final IDistributionUserService distributionUserService;

	private final MobilePhoneDesensitization mobilePhoneDesensitization = new MobilePhoneDesensitization();

	@Override
	public void before(UserLoginDTO userLoginDTO) {
	}

	@Override
	public UserInfo doLogin(UserLoginDTO userLoginDTO) {

		String clientType = userLoginDTO.getClientType();

		UserInfo userInfo = userInfoService.getByPhone(userLoginDTO.getPhone());
		if (ObjectUtil.isNull(userInfo)) {
			userInfo = new UserInfo();
			userInfo.setPhone(userLoginDTO.getPhone());
			userInfo.setUserGrade(MallUserConstants.USER_GRADE_0);
			userInfo.setNickName(mobilePhoneDesensitization.serialize(userLoginDTO.getPhone()));
			userInfo.setAvatarUrl(MallUserConstants.DEFAULT_AVATAR_URL);
			userInfo.setAccountBalance(BigDecimal.ZERO);
			userInfo.setUserSource(clientType);
			userInfoService.save(userInfo);
		}

		if (ClientTypeEnum.WX_MA.getCode().equals(clientType) || ClientTypeEnum.ALI_MA.getCode().equals(clientType)) {
			String hxToken = userLoginDTO.getHxToken();
			HxTokenInfo hxTokenInfo = HxTokenHolder.getTokenInfo();
			String tokenKey = CacheConstants.MALL_USER_TOKEN + hxToken;
			// 更新redis中的token信息
			hxTokenInfo.setMallUserId(userInfo.getId());
			redisTemplate.opsForValue()
				.set(tokenKey, JSONUtil.toJsonStr(hxTokenInfo), CacheConstants.TOKEN_TIME, TimeUnit.HOURS);
			userInfo.setHxToken(StrUtil.removeAll(tokenKey, CacheConstants.MALL_USER_TOKEN));
		}
		else {
			String token = UUID.randomUUID().toString();
			// 将token和用户信息存入redis，并设置过期时间
			String tokenKey = CacheConstants.MALL_USER_TOKEN + token;
			HxTokenInfo hxTokenInfo = new HxTokenInfo();
			hxTokenInfo.setClientType(userLoginDTO.getClientType());
			hxTokenInfo.setMallUserId(userInfo.getId());
			hxTokenInfo.setTenantId(userInfo.getTenantId());
			redisTemplate.opsForValue()
				.set(tokenKey, JSONUtil.toJsonStr(hxTokenInfo), CacheConstants.TOKEN_TIME, TimeUnit.HOURS);
			userInfo.setHxToken(StrUtil.removeAll(tokenKey, CacheConstants.MALL_USER_TOKEN));
		}
		return userInfo;
	}

	@Override
	public void updateUser(UserInfo userInfo) {
		userInfoService.updateById(userInfo);
	}

	@Override
	public void distributionBindUser(UserInfo userInfo) {
		distributionUserService.bindUser(userInfo);
	}

}
